package com.jiyinit.web.core.business.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.FunctionPageRequest;
import com.jiyinit.web.core.business.dto.FunctionRequest;
import com.jiyinit.web.core.business.entity.TabCoreFunEntity;
import com.jiyinit.web.core.business.entity.TabCoreGroupEntity;
import com.jiyinit.web.core.business.entity.TabCoreOrgEntity;
import com.jiyinit.web.core.business.entity.TabCoreResourceEntity;
import com.jiyinit.web.core.business.enums.ResourceTypeEnum;
import com.jiyinit.web.core.business.service.FunctionServiceImpl;
import com.jiyinit.web.core.business.vo.FunctionVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lr on 2017/12/20.
 */
@Controller
@RequestMapping("/fun")
public class FunctionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(FunctionController.class);

    @Resource(name = "function.serviceImpl")
    private FunctionServiceImpl functionServiceImpl;


    @RequestMapping("/tofunList")
    public String list(Model model){
        return "function/list";
    }

    @RequestMapping("/findPageByParentId")
    @ResponseBody
    public PagingResult findPageByParentId(FunctionPageRequest request){
        request.setLimit(10);
        PagingResult pagingResult = functionServiceImpl.findPageByParentId(request);
        LogUtil.info(LOGGER, "pagingResult:{}", JsonEntityTransform.object2Json(pagingResult));
        return pagingResult;
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer funId,Model model){
        TabCoreFunEntity function = functionServiceImpl.findByPK(funId);
        model.addAttribute("function",function);
        return "function/update";
    }
    @ResponseBody
    @RequestMapping("update")
    public ResponseEntity<ResponseDto> update(FunctionRequest functionRequest){
        int num = functionServiceImpl.update(functionRequest);
        if(num>0){
            return new ResponseEntity<>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("修改失败"),HttpStatus.OK);
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model,Integer parentId){
        model.addAttribute("parentId",parentId);
        return "function/add";
    }

    @ResponseBody
    @RequestMapping("add")
    public ResponseEntity<ResponseDto> add(HttpServletRequest request,FunctionRequest functionRequest){
        int num = functionServiceImpl.add(functionRequest);
        if(num>0){
            return new ResponseEntity<>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public ResponseEntity<ResponseDto> delete(HttpServletRequest request,Integer[] funIds){
        int num = functionServiceImpl.delete(funIds);
        if(num>0){
            return new ResponseEntity<>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("删除失败"),HttpStatus.OK);
    }



    /**
     * 查询功能树
     * @author changly
     * @create 2017/12/21 15:16
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/getTreeByParent")
    public ResponseEntity<ResponseDto> getTreeByParent(Integer parentId){
        if(Check.isNullObj(parentId)){
            parentId = 0;
        }
        List<FunctionVo> funs = functionServiceImpl.selectByParentId(parentId);
        List items = new ArrayList<>();
        for (FunctionVo fun : funs) {
            Map map = new HashMap();
            map.put("id",fun.getFunId());
            map.put("pId", fun.getParentId());
            map.put("name", fun.getFunName());
            if(fun.getNum().intValue() > 0 ){
                map.put("isParent", true);
            } else {
                map.put("isParent", false);
            }
            items.add(map);
        }
        return new ResponseEntity<>(ResponseDto.responseOK(items), HttpStatus.OK);
    }

    /**
     * 删除校验
     * @param request
     * @param funIds
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/funDeleteCheck")
    public ResponseEntity<ResponseDto> orgDeleteCheck(HttpServletRequest request, Integer[] funIds) {
        LogUtil.info(LOGGER, "funIds:{}", funIds);
        List<TabCoreFunEntity> orgList = functionServiceImpl.selectByParentIds(funIds);
        if (Check.isNullCollection(orgList)) {
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail(""), HttpStatus.OK);
    }


}
